package com.mystyle.le.qjnewsproject.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mystyle.le.qjnewsproject.R;
import com.mystyle.le.qjnewsproject.adapter.NewsAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewsFragment extends Fragment {
    private ViewPager viewPager;
    private ViewPager viewPage;
    private String[] titles = {"最新","硬件","软件","手机","科技"};
    private List<Fragment> list = new ArrayList<>();
    private int widthPixels;
    private RelativeLayout title_layout;
    private ImageView imageView;

    public NewsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        widthPixels = getActivity().getResources().getDisplayMetrics().widthPixels;
        initView(view);
        initTitle(view);
        return view;
    }
/*
* 初始化标签
* */
    private void initTitle(View view) {
        title_layout = ((RelativeLayout) view.findViewById(R.id.title_layout));
        imageView = new ImageView(getActivity());
        imageView.setImageResource(R.drawable.umeng_socialize_oauth_check_off);
        /*
        * 将图片先添加到布局中
        * */
        title_layout.addView(imageView);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        LinearLayout linearLayout = new LinearLayout(getActivity());
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setLayoutParams(layoutParams);
//        添加标签
        for (int i=0;i<titles.length;i++){
            LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0,ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams1.weight = 1;
            TextView textView = new TextView(getActivity());
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.BLACK);
            textView.setLayoutParams(layoutParams1);
            textView.setText(titles[i]);
            linearLayout.addView(textView);
        }
        title_layout.addView(linearLayout);
    }

    private void initView(View view) {
        viewPage = ((ViewPager) view.findViewById(R.id.news_vp));
        News_Est_Fragment estFragment = new News_Est_Fragment();
        News_HardWare_Fragment hardWare_fragment = new News_HardWare_Fragment();
        News_SortWore_Fragment sortWore_fragment = new News_SortWore_Fragment();
        News_Phone_Fragment phone_fragment = new News_Phone_Fragment();
        News_Scinece_Fragment scinece_fragment = new News_Scinece_Fragment();
        list.add(estFragment);
        list.add(hardWare_fragment);
        list.add(sortWore_fragment);
        list.add(phone_fragment);
        list.add(scinece_fragment);
        NewsAdapter adapter = new NewsAdapter(getChildFragmentManager(),list);
        viewPage.setAdapter(adapter);
        viewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int imagePosition = position * (widthPixels / 5) + positionOffsetPixels / 5;
                //设置当前imageView的位置
                imageView.setX(imagePosition);
            }

            @Override

            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

}
